package cn.edu.scau.daoImpl;


import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import cn.edu.scau.dao.UserDao;
import cn.edu.scau.entity.Blog;
import cn.edu.scau.entity.User;

@Repository("userDaoImpl")
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
	
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	public List<Blog> findUserBlogsByType(Integer userId, Integer pageNo, Integer pageSize,Integer type) {
		
		Session session = sessionFactory.openSession();
		User user = session.load(User.class, userId);
		String condition = "order by this.blogId desc";
		if(type==1){
			List<Blog> userBlogs = session.createFilter(user.getBlogs(), condition)
					.setFirstResult((pageNo-1)*pageSize)
					.setMaxResults(pageSize)
					.list();
			session.flush();
			session.close();
			return userBlogs;
		}else{
			List<Blog> userColBlogs = session.createFilter(user.getCollection(), condition)
					.setFirstResult((pageNo-1)*pageSize)
					.setMaxResults(pageSize)
					.list();
			session.flush();
			session.close();
			return userColBlogs;
		}
	
		
	}

	public long getBlogsMaxPageByType(Integer userId, Integer pageSize, Integer type) {
		
		Session session = sessionFactory.openSession();
		if(type==1){
			User user = session.load(User.class, userId);
			long count = user.getBlogs().size();
			session.flush();
			session.close();
			if(count==0){
				return 0;
			}else{
				if(count%pageSize==0){
					return count/pageSize;
				}else{
					return count/pageSize+1;
				}
			}
		}else{
			User user = session.load(User.class, userId);
			long count = user.getCollection().size();
			session.flush();
			session.close();
			if(count==0){
				return 0;
			}else{
				if(count%pageSize==0){
					return count/pageSize;
				}else{
					return count/pageSize+1;
				}
			}
		}
	}

	public void addUserBlog(Integer userId, Blog blog) {
		Session session = sessionFactory.openSession();
		User user = session.load(User.class, userId);
		Set<Blog> userBlogs = user.getBlogs();
		userBlogs.add(blog);
		session.flush();
		session.close();
	}

	public boolean addUserColBlog(Integer userId, Integer blogId) {
		Session session = sessionFactory.openSession();
		List<Blog> colBlogs = session.createQuery("select b from Blog b where b.blogId = ?")
				.setParameter(0, blogId)
				.list();
		if(colBlogs.size()==0){
			session.flush();
			session.close();
			return false;
		}else{
			User user = session.load(User.class, userId);
			Set<Blog> userColBlogs = user.getCollection();
			userColBlogs.add(colBlogs.get(0));
			session.flush();
			session.close();
			return true;
		}
	}

	public boolean deleteBlogByType(Integer userId, Integer blogId, Integer type) {
		Session session = sessionFactory.openSession();
		if(type==1){
			List<Blog> blogs = session.createQuery("select b from Blog b where b.blogId = ?")
					.setParameter(0, blogId)
					.list();
			if(blogs.size()==0){
				session.flush();
				session.close();
				return false;
			}else{
				User user = session.load(User.class, userId);
				if(blogs.get(0).getImageAdresses().size()!=0){
					session.createSQLQuery("delete from blog_image where blog_id = " + blogId).executeUpdate();
				}
				session.createSQLQuery("delete from user_blog where blog_id = "+blogId).executeUpdate();
				session.createSQLQuery("delete from user_collection where blog_id = "+blogId).executeUpdate();
				session.createQuery("delete Blog b where b.blogId = "+blogId).executeUpdate();
				session.flush();
				session.close();
				return true;
			}
			
		}else{
			List<Blog> colBlogs = session.createQuery("select b from Blog b where b.blogId = ?")
					.setParameter(0, blogId)
					.list();
			if(colBlogs.size()==0){
				session.flush();
				session.close();
				return false;
			}else{
				User user = session.load(User.class, userId);
				Set<Blog> userColBlogs = user.getCollection();
				userColBlogs.remove(colBlogs.get(0));
				session.flush();
				session.close();
				return true;
		}
	}
  }

}
